from django.shortcuts import render_to_response, get_object_or_404
from django.http import HttpResponseRedirect, get_host
from django.template import RequestContext
from django.core.urlresolvers import reverse
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.models import User
from django.contrib.auth.decorators import login_required

from videos.forms import VideoUploadForm, VideoEditForm
from projects.models import Project
from tribes.models import Tribe
from videos.models import Video

def upload(request, form_class=VideoUploadForm,
        template_name="videos/upload.html"):
    """
    upload form for videos
    """
    video_form = form_class()
    if request.method == 'POST':
        if request.POST["action"] == "upload":
            video_form = form_class(request.user, request.POST, request.FILES)
            if video_form.is_valid():
                video = video_form.save(commit=False)
                video.member = request.user
                video.save()
                request.user.message_set.create(message=_("Successfully uploaded video '%s'") % video.title)
                return HttpResponseRedirect(reverse('video_details', args=(video.id,)))

    return render_to_response(template_name, {
        "video_form": video_form,
    }, context_instance=RequestContext(request))
upload = login_required(upload)

def yourvideos(request, template_name="videos/yourvideos.html"):
    """
    videos for the currently authenticated user
    """
    videos = Video.objects.filter(member=request.user).order_by("-date_added")
    return render_to_response(template_name, {
        "videos": videos,
    }, context_instance=RequestContext(request))
yourvideos = login_required(yourvideos)

def videos(request, template_name="videos/latest.html"):
    """
    latest videos
    """
    videos = Video.objects.filter(is_public=True).order_by("-date_added")
    return render_to_response(template_name, {
        "videos": videos,
    }, context_instance=RequestContext(request))
videos = login_required(videos)

def details(request, id, template_name="videos/details.html"):
    """
    show the video details
    """
    other_user = get_object_or_404(User, username=request.user.username)
    video = get_object_or_404(Video, id=id)
    return render_to_response(template_name, {
        "video": video,
        "other_user": other_user, 
    }, context_instance=RequestContext(request))
details = login_required(details)

def membervideos(request, username, template_name="videos/membervideos.html"):
    """
    Get the members videos and display them
    """
    user = get_object_or_404(User, username=username)
    videos = Video.objects.filter(member__username=username, is_public=True).order_by("-date_added")
    return render_to_response(template_name, {
        "videos": videos,
    }, context_instance=RequestContext(request))
membervideos = login_required(membervideos)

def edit(request, id, form_class=VideoEditForm,
        template_name="videos/edit.html"):
    video = get_object_or_404(Video, id=id)
    video_url = video.video.url

    if request.method == "POST":
        if video.member != request.user:
            request.user.message_set.create(message="You can't edit videos that aren't yours")
            return HttpResponseRedirect(reverse('video_details', args=(video.id,)))
        if request.POST["action"] == "update":
            video_form = form_class(request.user, request.POST, instance=video)
            if video_form.is_valid():
                videoobj = video_form.save(commit=False)
                videoobj.save()
                request.user.message_set.create(message=_("Successfully updated video '%s'") % video.title)
                                
                return HttpResponseRedirect(reverse('video_details', args=(video.id,)))
        else:
            video_form = form_class(instance=video)

    else:
        video_form = form_class(instance=video)

    return render_to_response(template_name, {
        "video_form": video_form,
        "video": video,
        "video_url": video_url,
    }, context_instance=RequestContext(request))
edit = login_required(edit)

def destroy(request, id):
    video = Video.objects.get(pk=id)
    title = video.title
    if video.member != request.user:
        request.user.message_set.create(message="You can't delete videos that aren't yours")
        return HttpResponseRedirect(reverse("videos_yours"))

    if request.method == "POST" and request.POST["action"] == "delete":
        video.delete()
        request.user.message_set.create(message=_("Successfully deleted video '%s'") % title)
    return HttpResponseRedirect(reverse("videos_yours"))
destroy = login_required(destroy)
